package com.raccoon.ibatis.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import sun.print.resources.serviceui;

import com.raccoon.ibatis.manager.IBatisManager;
import com.raccoon.ibatis.manager.IBatisManagerImpl;


public class MultipleIbatisService {

	private static MultipleIbatisService		instance;
	private static Map<String, IBatisManager>	managerMap;
	
	private MultipleIbatisService() {
		managerMap = new HashMap<String, IBatisManager>();
	}
	
	/**
	 * 
	 * @return
	 */
	public static MultipleIbatisService getInstance() {
		if (instance == null) {
			instance = new MultipleIbatisService();
		}
		
		return instance;
	}
	
	/**
	 * 
	 * @param configFile
	 * @return
	 */
	public static IBatisManager getIBatisManager(String configFile) {
		IBatisManager	manager = managerMap.get(configFile);
		
		if ( manager == null) {
			manager = new IBatisManagerImpl(configFile);
			manager.setEnable(true);
			managerMap.put(configFile, manager);
		}
		
		return manager;
	}
	
	/**
	 * 
	 * @param configFile
	 */
	public static void destoryIBatisManager(String configFile) {
		IBatisManager	manager = managerMap.get(configFile);
		
		if (manager != null) {
			manager = null;
			managerMap.remove(configFile);
		}
	}
	
	/**
	 * 
	 * @param configFile
	 */
	public static void enableIBatisManager(String configFile) {
		getIBatisManager(configFile).setEnable(true);
	}
	
	/**
	 * 
	 * @param configFile
	 */
	public static void disableIBatisManager(String configFile) {
		getIBatisManager(configFile).setEnable(false);
	}
	
	/**
	 * 
	 * @return
	 */
	public static int getManagerCount() {
		return managerMap.size();
	}
	
	/**
	 * 
	 * @return
	 */
	public static Iterator<String> getManagerKeys() {
		return (Iterator<String>) managerMap.keySet().iterator();
	}
}